package dboperation;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.teneo.hibernate.HbDataStore;
import org.eclipse.emf.teneo.hibernate.HbHelper;

import vehiculeEMF.VehiculeEMFPackage;
import vehiculeEMF.craft.CraftPackage;
import vehiculeEMF.landvehicule.LandvehiculePackage;

public final class HibernateManagement {

	private static HbDataStore HIBERNATE_STORE = null;

	// Recuperation du DataStore <<Singleton>>
	public static final HbDataStore getDataStore() {
		if (HIBERNATE_STORE == null) {
			Properties props = new Properties();

			InputStream in = null;
			try {
				in = new FileInputStream("hibernate.properties");
				props.load(in);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

			// Create dataStore
			final String dataStoreName = "VehiculeDataShore";
			HIBERNATE_STORE = HbHelper.INSTANCE
					.createRegisterDataStore(dataStoreName);
			//

			HIBERNATE_STORE.setProperties(props);

			// Configure the EPackages used by this dataStore
			HIBERNATE_STORE.setEPackages(new EPackage[] {
					VehiculeEMFPackage.eINSTANCE, CraftPackage.eINSTANCE,
					LandvehiculePackage.eINSTANCE });

			// Initialize the DataStore. This sets up the Hibernate mapping and
			// creates the corresponding tables in the database.
			HIBERNATE_STORE.initialize();
		}

		return HIBERNATE_STORE;
	}

}
